package com.pcafs.manage.service.impl;

import com.pcafs.manage.mapper.PcafsDataCountMapper;
import com.pcafs.manage.service.IPcafsDataCountService;
import com.pcafs.manage.util.PCA;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: hyh
 * @Date: 2022/5/11 14:48
 **/
@Service
@AllArgsConstructor
public class PcafsDataCountImpl implements IPcafsDataCountService {

    private PcafsDataCountMapper mapper;

    @Override
    public List<Map> storeCountByYear() {
        return mapper.storeCountByYear();
    }

    @Override
    public List<Map> getStoreInfo() {
        return null;
    }

    @Override
    public List<Map> storeCountByProvince() {
        return mapper.storeCountByProvince();
    }

    @Override
    public List<Map> storeDetailCount() {
        return mapper.storeDetailCount();
    }

    @Override
    public List<Map> storeCountBySum() {
        List<Map> mapList = mapper.storeCountByYear();
        for (int i = 0; i < mapList.size(); i++) {
            int sum = 0;
            for (int j = i; j >= 0; j--) {
                String fsv = mapList.get(j).get("fsv").toString();
                sum = sum + Integer.parseInt(fsv);
            }
            Map map = mapList.get(i);
            map.put("sum", sum);
        }
        return mapList;
    }

    @Override
    public List<Map> storeIncomeByTime(Integer type, Long id) {
        if (type == 1) {
            return mapper.storeIncomeByYear(id);
        }
        if (type == 2) {
            return mapper.storeIncomeByQuarter(id);
        }
        if (type == 3) {
            return mapper.storeIncomeByMonth(id);
        }
        return null;
    }

    @Override
    public List<Map> storeCostByTime(Integer type) {
        if (type == 1) {
            return mapper.costByYear();
        }
        if (type == 2) {
            return mapper.costByQuarter();
        }
        return null;
    }

    @Override
    public List<Map> analyzeByTime(Integer type) {
        if (type == 1) {
            return mapper.analyzeByYear();
        }
        if (type == 2) {
            return mapper.analyzeByQuarter();
        }
        return null;
    }

    @Override
    public List<Map> analyzeInfo(Integer type) {
        return mapper.analyzeInfo(type);
    }

    @Override
    public double[][] qzxhData(Integer type) {
        List<LinkedHashMap> maps = mapper.analyzeJzInfo(type);
        PCA pca = new PCA();
        double[][] averageArray = pca.ysData(maps);
        return averageArray;
    }

    @Override
    public double[][] xfcData(Integer type) {
        List<LinkedHashMap> maps = mapper.analyzeJzInfo(type);
        PCA pca = new PCA();
        double[][] varMatrix = pca.xfcData(maps);
        return varMatrix;
    }
    @Override
    public double[][] tzzjzData(Integer type) {
        List<LinkedHashMap> maps = mapper.analyzeJzInfo(type);
        PCA pca = new PCA();
        double[][] varMatrix = pca.tzzjzData(maps);
        return varMatrix;
    }
    @Override
    public double[][] tzzxlData(Integer type) {
        List<LinkedHashMap> maps = mapper.analyzeJzInfo(type);
        PCA pca = new PCA();
        double[][] varMatrix = pca.tzzxlData(maps);
        return varMatrix;
    }

    @Override
    public Object[][] zfcjs(Integer type) {

        if(type == 2020){
            Object[][] data = {
                    {"总计","方差百分比"},
                    {2.746,	45.762},
                    {1.843,	30.723},
                    {1.288,	21.467},
                    {0.093,	1.555},
                    {0.029,	0.491},
                    {9.315E-5,	0.002}
            };
            return data;
        }
        Object[][] data1 = {
                {"总计","方差百分比"},
                {4.228,	70.467},
                {0.883,	14.714},
                {0.691,	11.520},
                {0.186,	3.098},
                {0.012,	0.196},
                {0.000,	0.006}
        };
        return data1;
    }
    @Override
    public Object[][] zcf(Integer type) {

        if(type == 2020){
            Object[][] data = {
                    {"净资产收益率",  0.449	,0.842	,0.289},
                    {"资产负债率",  -0.915	,0.174	,0.344},
                    {"总资产收益率",  0.756	,0.631	,0.121},
                    {"销售净利率",  0.742	,-0.363	,0.532},
                    {"总资产周转率",  -0.573	,0.757	,-0.232},
                    {"存货周转率",  0.507	,0.013	,-0.857}
            };
            return data;
        }
        Object[][] data1 = {
                {"净资产收益率", 0.783},
                {"资产负债率",   -0.913},
                {"总资产收益率", 0.957},
                {"销售净利率",   0.937},
                {"总资产周转率", -0.788},
                {"存货周转率",   0.606}
        };
        return data1;
    }
}
